/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.features;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.osdtsystem.matrixparser.features.keys.FeatureKey;

/**
 *
 * @author Martin Haulrich
 */
public class MapFeatureHandler implements FeatureHandler {
    int nextNr;
    Map<Object, Feature> map;
    boolean grow;

    public MapFeatureHandler() {
        nextNr = 1;
        map = new HashMap<Object, Feature>();
        grow = true;
        map.put(UNKNOWNNAME, UNKNOWN);
    }

    public Feature getFeature(FeatureKey key) {
        Feature f = map.get(key);
        if (f == null) {
            if (!grow) {
                return UNKNOWN;
            }
            f = new Feature(nextNr);
            nextNr++;
            map.put(key.copy(), f);
        }
        return f;
    }

    public Feature getFeature(String key) {
        Feature f = map.get(key);
        if (f == null) {
            if (!grow) {
                return UNKNOWN;
            }
            f = new Feature(nextNr);
            nextNr++;
            map.put(key, f);
        }
        return f;
    }

    public void stopGrowth() {
        grow = false;
    }

    public int alphabetSize() {
        return map.size();
    }

    @Override
    public String toString() {
        String[] keys = map.keySet().toArray(new String[0]);
        Arrays.sort(keys);
        StringBuilder sb = new StringBuilder();
        sb.append(super.toString());
        sb.append("\n");
        for (String key : keys) {
            sb.append("\t").append(key).append("=")
                    .append(map.get(key)).append("\n");
        }
        return sb.toString();
    }




    public void write(String fn) throws IOException {
        PrintWriter out = new PrintWriter(fn);

        for (Object name : map.keySet()) {
            out.println(name.toString());//) + "\t" + map.get(name).id);
        }
        out.close();
    }

//    public void writeFVS(Iterator<CONLLSentence> sentenceIterator, EdgeFactoredFeatureExtractor extractor, String fn) throws IOException {
//
//        System.err.print("Creating FeatureVector instances...");
//        long a = System.currentTimeMillis();
//        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fn)));
//
//        while (sentenceIterator.hasNext()) {
//            CONLLSentence sen = sentenceIterator.next();
//            CONLLSentence senWithRoot = IO.addRoot(sen);
//            FeatureVector[][] fvs = FeatureUtils.fillFvs(senWithRoot, extractor);
//            IO.writeToFVSFile(out, fvs);
//
//        }
//        out.close();
//        long b = System.currentTimeMillis();
//        long t = (b - a) / 1000;
//        System.err.println("   [" + t + " seconds]");
//    }

    public void setAlphabetGrowth(boolean grow) {
        this.grow = grow;
    }
}
